package com.shutuo.callnummeiwei.core;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.shutuo.callnummeiwei.net.SocketWire;
import org.json.JSONException;
import org.json.JSONObject;
import ashy.earl.common.data.JsonParser;


public class C {
    public static final String CHANNEL = "channel";
    public static final int TYPE_CALLNUMBER = 1001;
    public static final int TYPE_CALLNUMBER_QUEUE = 1002;

    public static final String SERVER_STATE_ACTION = "server_state_action";
    public static final String SERVER_NEW_CLIENT_MSG = "server_new_client_msg";
    public static final String SERVER_CLIENT_STATE_ACTION = "server_client_state_action";
    public static final String CLIENT_STATE_ACTION = "client_state_action";
    public static final String CLIENT_NEW_PACKAGE_ACTION = "client_new_package_action";

    static final int MSG_HELLO = 1;
    static final int MSG_HELLO_ACK = 2;
    static final SocketWire.WireConfig WIRE_CONFIG = new SocketWire.WireConfig(new MyPacketLogger(),
            0x1000);

    public static final class MyPacketLogger extends SocketWire.DefaultPacketLogger {
        @Override
        public String getPacketLog(SocketWire.Packet packet, Object parsedObj) {
            if (packet.type == SocketWire.Packet.TYPE_MSG) {
                return packet.arg0 + "-" + parsedObj;
            }
            return super.getPacketLog(packet, parsedObj);
        }
    }

    // ==========================================================================================
    // Client-Server messages
    static class MsgHello {
        public final long clientVersion;
        public final String pkg;

        MsgHello(long clientVersion, String pkg) {
            this.clientVersion = clientVersion;
            this.pkg = pkg;
        }

        @Override
        public String toString() {
            return "MsgHello{" +
                    "clientVersion=" + clientVersion +
                    ", pkg='" + pkg + '\'' +
                    '}';
        }
    }

    static class MsgHelloAck {
        public final long serverVersion;

        MsgHelloAck(long serverVersion) {
            this.serverVersion = serverVersion;
        }

        @Override
        public String toString() {
            return "MsgHelloAck{" +
                    "serverVersion=" + serverVersion +
                    '}';
        }
    }

    static final JsonParser<MsgHello> PARSER_HELLO = new JsonParser<MsgHello>() {
        @Nullable
        @Override
        public MsgHello onParse(@NonNull JSONObject json) {
            return new MsgHello(json.optLong("clientVersion"), json.optString("pkg"));
        }

        @Nullable
        @Override
        public JSONObject toJson(@NonNull MsgHello obj) {
            try {
                return new JSONObject().put("clientVersion", obj.clientVersion)
                        .put("pkg", obj.pkg);
            } catch (JSONException e) {
                // Never happened
                throw new RuntimeException(e);
            }
        }

        @Override
        public MsgHello[] newArray(int size) {
            return new MsgHello[size];
        }
    };
    static final JsonParser<MsgHelloAck> PARSER_HELLO_ACK = new JsonParser<MsgHelloAck>() {
        @Nullable
        @Override
        public MsgHelloAck onParse(@NonNull JSONObject json) {
            return new MsgHelloAck(json.optLong("serverVersion"));
        }

        @Nullable
        @Override
        public JSONObject toJson(@NonNull MsgHelloAck obj) {
            try {
                return new JSONObject().put("serverVersion", obj.serverVersion);
            } catch (JSONException e) {
                // Never happened
                throw new RuntimeException(e);
            }
        }

        @Override
        public MsgHelloAck[] newArray(int size) {
            return new MsgHelloAck[size];
        }
    };
}
